package com.xxx.video.commen.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.core.view.MotionEventCompat;

import com.xxx.video.R;

public class FastIndexBar extends View {
    /**
     *
     */
    private Context context;
    /**
     *
     */
    private int Width;
    /**
     *
     */
    private int Height;
    /**
     *
     */
    private int TextColor;
    /**
     *
     */
    private int TextSize;
    /**
     *
     */
    private int BackgroundColor;

    /**
     *
     */
    private float cellHeight;
    private static final String[] WORDS = new String[]{"A", "B", "C", "D",
            "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q",
            "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
    /**
     *
     */
    private Paint paint;
    /**
     *
     */
    int index = -1;
    /**
     *
     */
    private OnLetterChangeListener onLetterChangeListener;
    private Rect bounds;

    /**
     *
     */
    public interface OnLetterChangeListener {
        /**
         * @param letter
         */
        void onLetterChange(String letter);
    }

    public void setOnLetterChangeListener(
            OnLetterChangeListener onLetterChangeListener) {
        this.onLetterChangeListener = onLetterChangeListener;
    }

    public FastIndexBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
        this.context = context;
    }

    public FastIndexBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        init(attrs, defStyleAttr);
    }

    /**
     *
     */
    private void init(AttributeSet attrs, int defStyleAttr) {
        /*
         */
        TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.FastIndexBar, defStyleAttr, 0);
        for (int i = 0; i < typedArray.getIndexCount(); i++) {
            int attr = typedArray.getIndex(i);
            if (attr == R.styleable.FastIndexBar_BackgroundColor) {
                BackgroundColor = typedArray.getColor(attr, Color.WHITE);
            } else if (attr == R.styleable.FastIndexBar_TextColor) {
                TextColor = typedArray.getColor(attr, Color.BLUE);
            } else if (attr == R.styleable.FastIndexBar_TextSize) {
                TextSize = typedArray.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 10, getResources().getDisplayMetrics()));
            }
        }
        typedArray.recycle();
        /*
         */
        paint = new Paint();
        paint.setColor(TextColor);
        paint.setTextSize(TextSize);
        Typeface font = Typeface.create(Typeface.DEFAULT_BOLD, Typeface.BOLD);
        paint.setTypeface(font);
        bounds = new Rect();
    }

    /**
     *
     */
    @Override
    protected void onDraw(Canvas canvas) {
        for (int i = 0; i < WORDS.length; i++) {
            String text = WORDS[i];
            float x = (int) (Width * 0.5f - paint.measureText(text) * 0.5f);
            if (bounds == null) {
                bounds = new Rect();
            }
            paint.getTextBounds(text, 0, text.length(), bounds);
            int textHeight = bounds.height();
            float y = (int) (cellHeight * 0.5f + textHeight * 0.5 + cellHeight
                    * i);
            //  paint.setColor(i == index ? Color.GRAY : Color.BLACK);
            canvas.drawColor(BackgroundColor);
            canvas.drawText(text, x, y, paint);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float y;
        int currentIndex;
        switch (MotionEventCompat.getActionMasked(event)) {
            case MotionEvent.ACTION_DOWN:
                y = event.getY();
                currentIndex = (int) (y / cellHeight);
                if (currentIndex >= 0 && currentIndex < WORDS.length) {
                    if (index != currentIndex) {
                        if (onLetterChangeListener != null) {
                            onLetterChangeListener
                                    .onLetterChange(WORDS[currentIndex]);
                        }
                        index = currentIndex;
                    }
                }
                break;
            case MotionEvent.ACTION_MOVE:
                y = event.getY();
                currentIndex = (int) (y / cellHeight);
                if (currentIndex >= 0 && currentIndex < WORDS.length) {
                    if (index != currentIndex) {
                        if (onLetterChangeListener != null) {
                            onLetterChangeListener
                                    .onLetterChange(WORDS[currentIndex]);
                        }
                        index = currentIndex;
                    }
                }
                break;
            case MotionEvent.ACTION_UP:
                index = -1;
//                if (onLetterChangeListener != null) {
//                    onLetterChangeListener
//                            .onUp();
//                }
                break;
            default:
                break;
        }
        postInvalidate();
        return true;
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        Width = getMeasuredWidth();
        Height = getMeasuredHeight();
        cellHeight = Height * 1.0f / WORDS.length;
    }
}
